/**
 * @version 1.0
 * @created 16 November 2010
 *
 * CommonServiceImplementor
 *
 * Copyright @ 2010 by CES
 * All Rights Reserved.
 * CES CONFIDENTIAL AND TRADE SECRET
 * 
 */
package com.ces.knacky.framework.services.implementor;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.ces.knacky.framework.dao.CommonDAO;
import com.ces.knacky.framework.model.IEntity;
import com.ces.knacky.framework.services.CommonService;

/**
 * This class is used to integrate services to actual DAO layer. The integration is done using 
 * CommonDAO interface.
 * 
 * @see CommonService
 * @see CommonDAO
 * @see IEntity
 * @author bipul_sinha
 *
 */

public class CommonServiceImplementor implements CommonService {
	
	/**
	 * Used to perform data access operation. It is initialized using dependency injection. 
	 */
	private CommonDAO commonDao;
    /**
     * It is used to add/save an object. The object should be of type IEntity. 
     * 
     * @param 	entity
     * 			Represents object to be added/saved to the database.
     */
	@Transactional
	public boolean add(IEntity entity) {
		return commonDao.add(entity);		
	}
	/**
     * It is used to save a transient IEntity object and return persistent object after database
     * operation.
     * 
     * @param 	IEntity
     * 			Transient object to be saved.
     * @return	IEntity
     *   		Persistent Id of the object after save operation.
     */
	public Integer save(IEntity entity) {
		return commonDao.save(entity);
	}
    /**
     * It is used to physically delete object from the database. It takes entity object and Id for
     * deletion.
     *   
     * @param 	entity
     * 			Represents the entity to be physically deleted.	
     */
	public boolean delete(IEntity entity) {
		return commonDao.delete(entity);
		
	}
	/**
	 * It takes a query as a parameter and return list of objects.
	 *  
	 * @param 	query
	 * 			Represents query to be executed.
	 * @return	List
	 * 			Represents list of IEntity objects.
	 */
	public List<? extends IEntity> list(String query) {
		return commonDao.list(query);
	}
	/**
     * It is used to load the persistent object from the database. It takes Id and the name of 
     * entity to be loaded.
     *  
     * @param 	clazz
     * 			Represents fully qualified name of the class of whose entity to be loaded.
     * @param 	Id
     * 			Represents Id of the entity to be loaded.
     * @return	IEntity
     * 			Represents the loaded persistent entity. 
     */
	public IEntity load(Class<? extends IEntity> clazz, Integer Id) {
		return commonDao.load(clazz, Id);
	}
    /**
     * It is used to update/modify an entity. It takes entity as a parameter to be updated.
     * 
     * @param 	entity
     * 			Represents entity to be modified/updated in the database.
     */
	public boolean update(IEntity entity) {
		return commonDao.update(entity);
	}
	/**
     * It is used to fetch results from the database using description field. It returns list of 
     * objects whose description matches in the DB with the specified parameter.
     * 
     * @param 	name
     * 			Represents POJO class from where the description will be matched. 
     * @param 	description
     * 			Represents the value of description field whose objects needs to be returned.
     * @return	List
     *			Represents list of objects fetched from the database. 			
     */
	public List<? extends IEntity> findByDescription(Class<? extends IEntity> clazz, 
			String description) {
		return commonDao.findByDescription(clazz, description);
	}

	/**
     * It is used to fetch only the first object which matches the criteria. It is similar to 
     * findByDescription() method however returns only the first object unlike list of objects.
     *  
     * @param 	name
     * 			Represents POJO class from where the description will be matched.
     * @param 	description
     * 			Represents the value of description field whose objects needs to be returned.
     * @return	IEntity
     * 			Represent the first object fetched from the DB.
     */
	public IEntity findFirstByDescription(Class<? extends IEntity> clazz,
			String description) {
		return commonDao.findFirstByDescription(clazz, description);
	}
	/**
	 * Returns commonDAO object used during dependency injection.
	 * 
	 * @return	CommonDAO
	 * 			Represents dao object initialized using dependency injection.
	 */
	public CommonDAO getCommonDao() {
		return commonDao;
	}
	
	/**
	 * It is used to set the common dao object.
	 *  
	 * @param 	commonDao
	 * 			Represents dao object initialized using dependency injection.
	 */
	public void setCommonDao(CommonDAO commonDao) {
		this.commonDao = commonDao;
	}
	
	@Override
	public int batchUpdate(String statement) {		
		return commonDao.batchUpdate(statement);
	}
	
	
}

